package com.faithbj.device.web.controller.admin;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.faithbj.device.model.configuration.Pager;
import com.faithbj.device.model.entity.Members;
import com.faithbj.device.model.entity.Roles;
import com.faithbj.device.service.MemberService;

/**
 * <p>Copyright: Copyright (c) 2011</p> 
 * 
 * <p>Company: www.faithbj.com</p>
 * 
 * @author 	faithbj
 * @date 	2012-05-22
 * @version 1.0
 */
@Controller
@RequestMapping("/faith/member")
public class AdminMemberController extends BaseAdminController {

	private static final long serialVersionUID = -5383463207248344967L;

	@Resource
	private MemberService memberService;
	
	private final String INPUT="admin/member_input";
	
	// 会员menu
	@RequestMapping("menu")
		public String member() {
			return "admin/menu_member";
		}

	// 列表
	@RequestMapping("list")
	public String list(ModelMap map,Pager pager) {
		pager = memberService.findByPager(pager);
		map.put("pager", pager);
		return "admin/member_list";
	}

	/**
	 * 会员查询
	 * 
	 * @param pagerVal
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/searchmember", method = RequestMethod.POST)
	public String searchmember(Members member,Pager pagerVal,ModelMap map) {
		pagerVal = memberService.findMembersByExample(
				member, pagerVal.getPageNumber(), pagerVal.getPageSize());
//		pagerVal.setPageNumber(3);
		map.put("pager", pagerVal);
		map.put("param", member);
//		map.put("pageSize", pageSize);
		return "admin/member_list";
	}
	
	
	
	// 删除
	@RequestMapping("delete")
	public String delete(ModelMap map,@RequestParam String id) {
		memberService.delete(id);
		return SUCCESS;
	}

	// 添加
	@RequestMapping("new")
	public String add(ModelMap map) {
		map.put("isAddAction", true);
		return INPUT;
	}

	// 编辑
	@RequestMapping("edit")
	public String edit(ModelMap map,@RequestParam String id) {
		Members member = memberService.load(id);
		map.put("isAddAction", false);
		map.put("member", member);
		return INPUT;
	}

	// 保存
	@RequestMapping("save")
	public String save(Members member,ModelMap map,HttpServletRequest request) {
        Members tm=memberService.getMemberByUsername(member.getUsername());
        if(tm!=null)
        {
            map.put("errorMessages", "此用户已经存在，请使用其他用户名！");
            return ERROR;
        }

		Set<Roles> roles=new HashSet<Roles>();
		Roles role=new Roles();
		role.setId("402880e4378db0e901378db0f3650002");
		role.setName("user");
		role.setValue("ROLE_USER");

		Set<Members> members=new HashSet<Members>();
		members.add(member);
		role.setMembers(members);

		roles.add(role);

		member.setUsername(member.getUsername().trim());
		member.setAccountNonLocked(true);
		member.setLoginFailureCount(0);
		member.setLockedDate(null);
		member.setLoginDate(new Date());
		member.setRegisterIp(request.getRemoteAddr());
		member.setLoginIp(request.getRemoteAddr());

		member.setRoles(roles);

		memberService.save(member);
		
		map.put("redirectUrl", "/faith/member/list");
		return SUCCESS;
	}
	
	
	// 编辑个人资料
	@RequestMapping("/editself")
	public String editself(ModelMap map) {
		Members member = memberService.getMemberByUsername(getLoginMember().getUsername());
		map.put("member", member);
		return "admin/member_selfinput";
	}
	

	// 更新
	@RequestMapping(value="update", method = RequestMethod.POST)
	public String update(Members member,ModelMap map) {
		Members persistent = memberService.getMemberByUsername(member.getUsername());

		persistent.setRealName(member.getRealName().trim());
		persistent.setPassword(member.getPassword().trim());
		persistent.setEmail(member.getEmail().trim());
		persistent.setTelephone(member.getTelephone());
		persistent.setSafeAnswer(member.getSafeAnswer().trim());
		persistent.setSafeQuestion(member.getSafeQuestion().trim());
		
		memberService.update(persistent);
		
		map.put("redirectUrl", "/faith/member/list");
		return SUCCESS;
	}

}
